package com.dragon.utilslibrary.dblib.dbutil;



import com.dragon.utilslibrary.tools.LogUtils;

import java.lang.reflect.Method;

/**
 * 反射的getter和setter
 */
public class PropertyDescriptor {
    private static final String TAG = "PropertyDescriptor";
    /*
     *@param obj 操作的对象
     *@param att 操作的属性
     *@param value 设置的值
     *@param type 参数的类型
     * setter(obj, "name", "张泽", String.class);
			setter(obj, "age", 18, int.class);
			getter(obj, "name");
			getter(obj, "age");
     */
    public static void setter(Object obj, String att, Object value, Class<?> type){
        try {
            Method met = obj.getClass().getMethod("set" + initStr(att), type);
            met.setAccessible(true);
            met.invoke(obj, value);
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.e(TAG,"反射报错 setter (" + att + " ) " + " class = " + obj.getClass().getName());
        }
    }
    public static Object getter(Object obj, String att){
        try {
            Method met = obj.getClass().getMethod("get" + initStr(att));
            met.setAccessible(true);
            return met.invoke(obj);
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.e(TAG,"反射报错 getter (" + att + " ) " + " class = " + obj.getClass().getName());
        }
        return null;
    }
    public static String initStr(String old){	// 将单词的首字母大写
        String str = old.substring(0,1).toUpperCase() + old.substring(1) ;
        return str ;
    }
}
